package reflect.filed;

import java.lang.reflect.Field;

public class Demo {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class<?> aClass = Class.forName("reflect.filed.Student");
        //获取public修饰的成员变量，封装为一个数组
        Field[] fields = aClass.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
        //获取public修饰的成员变量，参数传递成员变量名
        Field field = aClass.getField("id");
        System.out.println(field);
        System.out.println("============================");
        //获取所有的成员变量
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }
        //获取单个成员变量
        Field name = aClass.getDeclaredField("name");
        System.out.println(name);
    }
}
